#include "../stdafx.h"
#include "UCN2IC10Windows.h"
#include "UCN2IC10Implementation.h"

#include "../ULeafWindow.h"
#include "../UNonSpaceFillingContainerWindow.h"
#include "../Controls/UMemo.h"
#include "../Controls/UEdit.h"
#include "../Controls/UTextButton.h"

#include <string>
#include <iostream>
#include <sstream>

using namespace std;

static bool ticker = false;
static Uint32 timerNetTickCallback(Uint32 interval, void* param) {
	ticker = true;
	return(interval);
}

TCN2IC10MainWindow::TCN2IC10MainWindow()
{
	impl = new CN2IC10Implementation(this);
	SDL_TimerID timer = SDL_AddTimer(100, timerNetTickCallback, NULL);
	Controls.AddControl(&impl->onlineUsers);
	Controls.AddControl(&impl->offlineUsers);
	Controls.AddControl(&impl->buttonChangeUserName);
	Controls.AddControl(&impl->buttonManualUserAdd);
	Controls.AddControl(&impl->editUserName);
	Controls.AddControl(&impl->editUserAdd);
}

void TCN2IC10MainWindow::Draw( ) {
	if (ticker) {
		impl->Ticker();
		ticker = false;
	}
	TControlContainerWindow::Draw();
}

void TCN2IC10MainWindow::resize(int w, int h)
{
	impl->editUserName.setPos(TRectangle(2,2,w/2-2,22));
	impl->editUserAdd.setPos(TRectangle(w/2+2,2,w-2,22));

	impl->buttonChangeUserName.setPos(TRectangle(2,24,w/2-2,40));
	impl->buttonManualUserAdd.setPos(TRectangle(w/2+2,24,w-2,40));

	impl->onlineUsers.setPos(TRectangle(2,42,w/2-2,h-2));
	impl->offlineUsers.setPos(TRectangle(w/2+2,42,w-2,h-2));
}

TCN2IC10ChatWindow* TCN2IC10MainWindow::newChatWindow(string user)
{
	static int ipos = -1;
	ipos += 1;
	if (ipos == 5) ipos = 0;
	int rpos = ipos*30;
	TCN2IC10ChatWindow* Window = new TCN2IC10ChatWindow(impl, user);
	TLeafWindow* BorderWindow = new TLeafWindow(TRectangle(), Window, this->Parent->GetRoot(), this->Parent->getWindowThemer(), string() + "Chat - " + user);
	Window->Parent = BorderWindow;
	BorderWindow->SetPos(TRectangle(50+rpos, 50+rpos, 250+rpos, 250+rpos));
	this->Parent->GetRoot()->AddChild(BorderWindow);
	return Window;
}

void sendfileproxy(CN2IC10Implementation* aimpl, string auser, TEdit* edit)
{
	aimpl->SendFileMessage(auser, edit->GetText());
	edit->SetText("");
}

TCN2IC10ChatWindow::TCN2IC10ChatWindow(CN2IC10Implementation* aimpl, string auser)
: impl(aimpl), user(auser)
{
	textedit = new TEdit(TRectangle());
	textmemo = new TMemo(TRectangle(), TGLFontRef(), RGBAColor(0xff0000), RGBAColor(0xa0a0a0));
	button = new TTextButton("Send as file", TGLFontRef(), TRectangle());
	textmemo->reverseoutline = true;
	textedit->clearOnEnter = true;
	Controls.AddControl(textedit);
	Controls.AddControl(textmemo);
	Controls.AddControl(button);

	textedit->onEnter = boost::bind(
		&CN2IC10Implementation::SendTextMessage, impl,
		boost::bind(
			&TCN2IC10ChatWindow::GetUser, this
		),
		boost::bind(
			&TEdit::GetText, textedit
		)
	);

	button->onClick = boost::bind(
		&sendfileproxy, impl,
		boost::bind(
			&TCN2IC10ChatWindow::GetUser, this
		),
		textedit
	);
}

void TCN2IC10ChatWindow::resize(int w, int h)
{
	textmemo->setPos(TRectangle(2, 2, w-2, h-24));
	textedit->setPos(TRectangle(2, h-22, w-100, h-2));
	button->setPos(TRectangle(w-98, h-22, w-2, h-2));
}

void TCN2IC10ChatWindow::SetUser(std::string auser)
{
	user = auser;
	Parent->Name = string("Chat - ") + auser;
}

std::string TCN2IC10ChatWindow::GetUser()
{
	return user;
}
